<template>
  <view style="display: flex; justify-content: center; padding: 20px">
    <wd-card>
      <view
        style="
          height: calc(90vh - 20px);
          width: 85vw;
          margin-top: 30px;
          display: flex;
          flex-direction: column;
          align-items: center;
        "
      >
        <wd-img :width="230" :height="150" :src="tipsImage" />
        <view style="text-align: center">
          根据您的性别、年龄、身高、体重、等身体数据，我为您生成的健康建议为：
        </view>
        <view v-if="loading">
          <wd-loading />
          AI分析中，请耐心等待
        </view>
        <view v-else style="padding: 10px; overflow: auto">
          <towxml :nodes="output"/>
        </view>
      </view>
    </wd-card>
  </view>
</template>
<script setup lang="js">
import tipsImage from "@/static/images/tips.jpg";
import UserAPI from "@/api/system/user";
const apiKey = import.meta.env.VITE_APP_DEEPSEEK_KEY;
const { proxy } = getCurrentInstance();
const output = ref("");
const loading=ref(false)
onMounted(() => {
  main();
});

async function main() {
  try {
    loading.value=true
    const { gender,age,height,weight,bust,waist,hip,arm,thigh,calf } = await UserAPI.getProfile();
    let message=`您好，我想请你帮我生成健康建议和营养知识，简短点，200字以内，这些是我的身体数据：性别：${gender},年龄：${age}岁,身高：${height},体重：${weight},胸围：${bust},腰围：${waist},臀围：${hip},手臂围：${arm},大腿围：${thigh},小腿围：${calf},`
    const response=await uni.request({
      url: 'https://api.deepseek.com/v1/chat/completions', // 替换为你的接口
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`// 替换为实际Key
      },
      data: {
        model: "deepseek-chat",
        messages: [{ role: "user", content: message }],
      },
    })
    output.value=proxy.$towxml(response.data.choices[0].message.content, 'markdown');
  } catch (error) {
    console.error("请求异常:", error);
    output.value = "请求异常，请检查控制台";
  } finally {
    loading.value=false
  }
}
</script>

<style scoped lang="scss"></style>
